<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<script type="text/javascript">
    var orderDrugEditor = new baidu.editor.ui.Editor();
	$(function() {

		$('#admin_order_drug_datagrid').datagrid({
			url : '${pageContext.request.contextPath}/orderDrugController/datagrid.action',
			fit : true,
			fitColumns : true,
			border : false,
			pagination : true,
			idField : 'id',
			pageSize : 10,
			pageList : [ 10, 20, 30, 40, 50 ],
			sortName : 'createTime',
			sortOrder : 'desc',
			checkOnSelect : false,
			selectOnCheck : false,
			nowrap : false,
			frozenColumns : [ [ {
				field : 'id', title : '编号', checkbox : true}, {
				field : 'orderId', title : '订单号', sortable : true}, {
				field : 'cardId', title : '卡号', sortable : true} ] ],
			columns : [ [ {
				field : 'name', title : '姓名', sortable : true}, {
				field : 'createTime', title : '创建时间', sortable : true}, {
				field : 'mobile', title : '手机号', sortable : true}, {
				field : 'statusName', title : '状态', sortable : true}, {
				field : 'cid', title : '身份证号', sortable : true}, {
				field : 'bornDate', title : '出生日期', sortable : true}, {
				field : 'genderName', title : '性别', sortable : true}, {
				field : 'serviceTime', title : '申请服务时间', sortable : true}, {
				field : 'factory', title : '生产厂家', sortable : true}, {
				field : 'dealer', title : '经销商', sortable : true}, {
				field : 'isUrgentName', title : '是否紧急', sortable : true}, {
				field : 'isNeedInvoiceName', title : '是否需要发票', sortable : true}, {
				field : 'isNeedDeliverName', title : '是否需要配送', sortable : true}, {
				field : 'action1', title : '查看详情/修改', align : 'center',
				formatter : function(value, row, index) {
				    return formatString('<img onclick="admin_order_drug_editFun(\'{0}\');" src="{1}"/ title="查看详情/修改">', row.id, '${pageContext.request.contextPath}/style/images/extjs_icons/pencil.png');
				}
			}]],
			toolbar : [ {
				text : '添加订单',
				iconCls : 'icon-add',
				handler : function() {
					admin_order_drug_appendFun();
				}
			}]
		});

	});

	function admin_order_drug_searchFun() {
		$('#admin_order_drug_datagrid').datagrid('load', serializeObject($('#admin_order_drug_searchForm')));
	}
	
	function admin_order_drug_cleanFun() {
		$('#admin_order_drug_searchForm input').val('');
		$('#admin_order_drug_datagrid').datagrid('load', {});
	}
	
	function admin_order_drug_editFun(id) {
		var index = $('#admin_order_drug_datagrid').datagrid('getRowIndex', id);
		var rows = $('#admin_order_drug_datagrid').datagrid('getRows');
		var o = rows[index];
		var data=dataResult(o.status,'${pageContext.request.contextPath}/orderDrugController/save_or_submit.action','${pageContext.request.contextPath}/orderDrugController/edit.action',
				'${pageContext.request.contextPath}/orderDrugController/cancel.action')
            $('#admin_order_drug_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
            $('<div/>').dialog({
                href : '${pageContext.request.contextPath}/admin/order_drug_edit.jsp',
                width : 800, height : 768, modal : true, title : '修改订单信息', buttons :data,
			onClose : function() {$(this).dialog('destroy');},
			onLoad : function() {
				$('#admin_order_drug_editForm').form('load', o);
				var formObj = $("#admin_order_drug_editForm");
				var orderId = formObj.find("input[name='orderId']").val();
				//生成药品信息
				$.ajax({
	        		type: 'get',
	        		url: '${pageContext.request.contextPath}/orderDrugController/item.action?orderId=' + orderId,
	        		async: false,
	        		success: function(data) {
	        			var r = $.parseJSON(data);
	            		if(r.success) {
	            			var drugItemsObj = formObj.find("input[name='drugItems']");
	            			drugItemsObj.val(r.obj.drugItems);
	            		} else {
	            			$.messager.alert('错误', r.msg);
	            		}
	        		},
	        		error: function () {
	        			$.messager.alert('错误','获取订单信息失败，请联系系统管理员！');
	        		}
	    		});
				
				var drugItemsStr = formObj.find("input[name='drugItems']").val();
				var htmlStr = "<table style='border:0px solid; width:98%'><tr><th colspan=5>需要购买的药品信息</th></tr><tr><th colspan=5><hr></th></tr><tr><th>名称</th><th>规格</th><th>剂型</th><th>数量</th><th>具体需求</th></tr>";
				var itemsArr = drugItemsStr.split(";");
				for(var i = 0; i < itemsArr.length; i++) {
					if($.trim(itemsArr[i]) != "") {
						htmlStr += "<tr style='border:1px solid' valign='top'>";
						var fields = itemsArr[i].split(",");
						htmlStr += "<td style='border:1px solid' align='center'>" + fields[0] + "</td>";
						htmlStr += "<td style='border:1px solid' align='center'>" + fields[1] + "</td>";
						htmlStr += "<td style='border:1px solid' align='center'>" + fields[2] + "</td>";
						htmlStr += "<td style='border:1px solid' align='center'>" + fields[3] + "</td>";
						htmlStr += "<td style='border:1px solid' align='center'>" + fields[4] + "</td>";
						htmlStr += "</tr>";
					}
				}
				htmlStr += "</table>";
				$("#divOrderDrugItems").html(htmlStr);
				
				//给baidu ueditor赋值
				orderDrugEditor.render("orderDrugDetailsEditor");
				orderDrugEditor.addListener("ready",function(){
					orderDrugEditor.setContent(formObj.find("input[name='serviceDetails']").val());
    			});
			}
		});
	}
	function currency(url,_this){
		var formObj = $("#admin_order_drug_editForm");
		formObj.find("input[name='serviceDetails']").val(orderDrugEditor.getContent());
		$('#admin_order_drug_editForm').form('submit', {
			url :url,
			success : function(result) {
				tsResult(result,'admin_order_drug_datagrid','.window-body','edit',_this)
			}
		});
	}
	function admin_order_drug_appendFun() {
		$('#admin_order_drug_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/admin/order_drug_add.jsp',
			width : 800,
			height : 700,
			modal : true,
			title : '添加订单',
			buttons : [ {
				text : '保存',
				iconCls : 'icon-add',
				handler : function() {
					var formObj = $("#admin_order_drug_addForm");
					var serviceTimeObj = formObj.find("input[name='serviceTime']");
					if(serviceTimeObj.val()=='') {
						$.messager.alert('错误','请填写申请服务时间！');
						return;
					}
					
					//得到药品信息
					var drugNameObjs = formObj.find("input[name='drugName']");
					var drugStandardObjs = formObj.find("input[name='drugStandard']");
					var drugTypeObjs = formObj.find("input[name='drugType']");
					var drugCountObjs = formObj.find("input[name='drugCount']");
					var drugRequirementObjs = formObj.find("textarea[name='drugRequirement']");
					
					var drugItemsStr = "";
					var prefix = "";
					for(var i = 0; i < drugNameObjs.length; i++) {
						if(i != 0) {
							prefix = ";";
						}
						if($.trim(drugNameObjs.eq(i).val()) != "") {
							if($.trim(drugCountObjs.eq(i).val()) == "") {
								$.messager.alert('错误','填写了药品名称后必须填写该药品所需的数量！');
								return;
							}
							drugItemsStr += prefix 
							+ $.trim(drugNameObjs.eq(i).val()) + ","
							+ $.trim(drugStandardObjs.eq(i).val()) + ","
							+ $.trim(drugTypeObjs.eq(i).val()) + ","
							+ $.trim(drugCountObjs.eq(i).val()) + ","
							+ $.trim(drugRequirementObjs.eq(i).val());
						}
					}
					
					//alert(drugItemsStr);
					if(drugItemsStr == "") {
						$.messager.alert('错误','必须填写至少一条药品信息，名称和数量必填！');
						return;
					}
					formObj.find("input[name='drugItems']").val(drugItemsStr);
					
					var d = $(this).closest('.window-body');
					$('#admin_order_drug_addForm').form('submit', {
						url : '${pageContext.request.contextPath}/orderDrugController/add.action',
						success : function(result) {
							try {
								var r = $.parseJSON(result);
								if (r.success) {
									$('#admin_order_drug_datagrid').datagrid('load',{sort : 'createTime', order : 'desc'});
									d.dialog('destroy');
								}
								$.messager.show({
									title : '提示',
									msg : r.msg
								});
							} catch (e) {
								$.messager.alert('提示', result);
							}
						}
					});
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			}
		});
	}
</script>
<div class="easyui-layout" data-options="fit : true,border : false">
	<div data-options="region:'north',title:'查询条件',border:false" style="height:220px" align="center">
		<form id="admin_order_drug_searchForm">
			<table class="tableForm">
			    <tr>
					<th style="width: 170px;">订单号(可模糊查询)</th>
					<td><input name="orderId" style="width: 315px;" /></td>
				</tr>
				<tr>
					<th style="width: 170px;">卡号(可模糊查询)</th>
					<td><input name="cardId" style="width: 315px;" /></td>
				</tr>
				<tr>
					<th style="width: 170px;">姓名(可模糊查询)</th>
					<td><input name="name" style="width: 315px;" /></td>
				</tr>
				<tr>
					<th style="width: 170px;">手机号(可模糊查询)</th>
					<td><input name="mobile" style="width: 315px;" /></td>
				</tr>
				<tr>
					<th>创建时间</th>
					<td><input name="createTimeStart" onFocus="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" />至<input name="createTimeEnd" onFocus="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" /></td>
				</tr>
			</table>
			<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="admin_order_drug_searchFun();">过滤</a> <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true" onclick="admin_order_drug_cleanFun();">取消过滤</a>
		</form>
	</div>
	<div data-options="region:'center',border:false">
		<table id="admin_order_drug_datagrid"></table>
	</div>
</div>